package com.khyr.service.impl;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.khyr.dao.DeptDao;
import com.khyr.service.DeptService;
import com.khyr.vo.DeptVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.entity.Dept;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 宋一恒
 * @version 1.0
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDao deptDao;
    @Override
    public R<Page<Dept>> list(DeptVo deptVo) {
        //分页查询
        Page<Dept> page = new Page<>(deptVo.getPageNumber(), deptVo.getPageSize());
        //模糊查询
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(deptVo.getDeptName())){
            queryWrapper.like("dept_name", deptVo.getDeptName());
        }
        if (StringUtils.hasText(deptVo.getStatus())){
            queryWrapper.eq("status", deptVo.getStatus());
        }
        //判断开始时间和结束时间是否为null
        if (StringUtils.hasText(deptVo.getBeginTime())&&StringUtils.hasText(deptVo.getEndTime())){
            queryWrapper.between("create_time",deptVo.getBeginTime(),deptVo.getEndTime());
        }
        Page<Dept> deptIPage = deptDao.selectPage(page, queryWrapper);
        return new R<Page<Dept>>(200,"查询成功",deptIPage);
    }

    @Override
    public R<String> deleteDeptByDeptIds(List<Long> ids) {
        int i = deptDao.deleteDeptByDeptIds(ids);
        if (i>0){
            return new R<String>(200,"删除成功",null);
        }
        return new R<String>(500,"删除失败",null);
    }

    @Override
    public R<Integer> addDept(Dept dept) {
        int i = deptDao.insert(dept);
        if (i>0){
            return new R<Integer>(200,"添加成功",null);
        }

        return new R<Integer>(500,"添加失败",null);
    }

    @Override
    public R<Integer> updateDept(Dept dept) {
        int i = deptDao.updateById(dept);
        if (i>0){
            return new R<Integer>(200,"修改成功",null);
        }
        return new R<Integer>(500,"修改失败",null);
    }

    @Override
    public R<Dept> getDeptByDeptId(Long deptId) {


        Dept dept = deptDao.selectById(deptId);
        if (dept!=null){
            return new R<Dept>(200,"查询成功",dept);
        }
        return new R<Dept>(500,"查询失败",null);
    }

    @Override
    //获取部门名称
    public R<List<Dept>> getDeptList() {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        //status=0未停用
        queryWrapper.eq("status","0");
        //del_flag=0未删除
        queryWrapper.eq("del_flag","0");
        List<Dept> list = deptDao.selectList(queryWrapper);
        if (list!=null) {
            return new R<List<Dept>>(200, "查询成功", list);
        }
        return new R<List<Dept>>(500, "查询失败", null);
    }


}
